package controllers.api;

import java.lang.reflect.Type;
import java.util.List;

import play.mvc.Controller;
import play.mvc.Http.StatusCode;
import com.google.gson.reflect.TypeToken;
import models.Notification;
import models.User;
import controllers.Data;

public class Notifications extends MeApi {
	
	public static void list(Integer offset, Integer limit) {
		if(offset == null) offset = 0;
		if(limit==null || limit > LIMIT || limit==0) {
			limit = LIMIT;
		}
		User current = getCurrentUser();
		Data<Notification> data = new Data<Notification>();
		List<Notification> notifications = Notification.paginate(Notification.all(current), offset, limit);
		data.total = Notification.count(current);
		data.data = notifications;
		Type dataType = new TypeToken<Data<Notification>>(){}.getType();
		renderJSON(gson().toJson(data, dataType));
	}
	
	public static void get(Long id) {
		Notification notification = Notification.findById(id);
		if(notification == null) notFound();
		User current = getCurrentUser();
		if(notification.owner.id.longValue() != current.id.longValue()) forbidden();
		renderJSON(gson().toJson(notification));
	}
	
	public static void delete(Long id) {
		Notification notification = Notification.findById(id);
		if(notification == null) notFound();
		User current = getCurrentUser();
		if(notification.owner.id.longValue() != current.id.longValue()) forbidden();
		notification.delete();
		response.status = StatusCode.NO_RESPONSE;
	}
	
}